<?php
/**
 * Members area welcome script
 * 
 * Tracks membership login into NATS3 database, checks the member's signup level
 * and redirects to apropriate membership area section
 * 
 * @name		welcome member
 * @version		1.1
 * @uses		Nats3space_Members
 * @package		nats-space
 * @see			http://code.google.com/p/nats-space/
 * @copyright	2008-2009
 * @author		Joseph C. <joseph.cheys@gmail.com>
 */

/**
 * Welcome Member - script configuration
 */

/**
 * Affects errors output
 */ 
define('DEBUG_MODE', false);

/**
 * Affiliate tracking system URL
 */
define('NATS_URL', 'http://nats.yourdomain.com/');

/**
 * Full membership area URL 
 */
define('FULL_URL', '/full/');

/**
 * Trial membership URL
 */
define('TRIAL_URL', '/trial/');

/**
 * NATS site ID values, if current member area server couple sites 
 * then this can be coma separated values, fo ex. 5,64,2
 */
define('SITE_IDS', '1');
 
/**
 * Library path 
 * (can be blank if this path is added into your inlude path)
 * NOTE! don't forget to add trailing slash
 */
define('LIB_DIR', ''); // set to /path/library/



/**
 * NATS config
 * You can define the path to NATS config file
 * and we will read the database accesses from that file  
 * OR... (to be continued, at the bottom  V )
 */
define('NATS_CONFIG', '/path-to-nats/nats/includes/config.php');

if (file_exists(NATS_CONFIG)) { // read database accesses
	
	require_once NATS_CONFIG; 
	define('DB_SERVER', $config['DB_SERVER']);
	define('DB_USER', $config['DB_USER']);
	define('DB_PASSWORD', $config['DB_PASSWORD']);
	define('DB_DB', $CONFIG['DB_DB']);
	
} else {
/**
 * ...OR you can provide the database accesses directly here
 */
	define('DB_SERVER',		'localhost');
	define('DB_USER',		'--databse user name --');
	define('DB_PASSWORD',	'--database password --');
	define('DB_DB',			'--database name--');
}










/**
 * END OF CONFIGURATION
 * 
 * WARNING!!! Do not modify the code above 
 * unleast you are realy know what you are doing.
 */

/**
 * Read required session/environment data
 */

/**
 * These values are preconfigured already 
 * so you don't need to change them
 * 
 * Possibly in next NATS varsion they will change
 * their full/trial memberhip level codes
 * but for now there is nothing to change
 */
define('NATS_MEMBERSHIP_LEVEL_TRIAL', 1);
define('NATS_MEMBERSHIP_LEVEL_FULL', 0);

/**
 * Asuming that this member was authenticated via mod_auth_mysql or similar
 */
define('MEMBER_LOGIN', $_SERVER['PHP_AUTH_USER']); 

/**
 * Get member IP address
 */
define('MEMBER_IP', $_SERVER['REMOTE_ADDR']);

/**
 * Track memberhsip login
 */
require_once LIB_DIR . 'Nats3space/Members.php'; // This is my class and I love it ;)
$result = Nats3space_Members::TrackLogin(
	  NATS_URL
	, MEMBER_LOGIN
	, MEMBER_IP
	, SITE_IDS
);

if (DEBUG_MODE && (false === $result)) {
	die(Nats3space_Members::getLastErrorMsg());
}

/**
 * Check membership level
 * @todo Use Zend_Db to avoid problems on new (cool!) hosts
 * 		 where only mysqli or/and PDO extensions available
 */
$dbLink = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DB, $dbLink);

$sql = '
	SELECT
		trial 
	FROM 
		members
	WHERE 
		username = ' . MEMBER_LOGIN . '
	ORDER BY
		memberid DESC';

$resDb = mysql_query($sql, $dbLink);
$rowData = mysql_fetch_array($resDb);

/**
 * In case session is not started automatically
 */
if (!isset($_SESSION)) {
	session_start();
}

/**
 * Store value into session for possible further use
 */
$_SESSION['trial'] = $rowData['trial'];

/**
 * Redirect member
 */
if (!DEBUG_MODE) {
	switch ($rowData['trial']) {
		
		case NATS_MEMBERSHIP_LEVEL_FULL:
			header('Location: ' . FULL_URL);
		break;
		
		case NATS_MEMBERSHIP_LEVEL_TRIAL:
			header('Location: ' . TRIAL_URL);
		break;
		
		default:
			header('Location: ' . TRIAL_URL);
	}
} else {
	die('Membership code: ' . $rowData['trial']);
}